//
//  PlaceholderTextView.swift
//  CocoProject
//
//  Created by Vill on 2023/11/13.
//

import UIKit

// 开始编辑
typealias editBeginblock = ()->Void

class PlaceholderTextView: UIView {

    //MARK: - 懒加载属性
    lazy var plaleLabel = UILabel()
    lazy var countLabel = UILabel()
    lazy var palceholdertextView = UITextView()
    
    var editBeginBack:editBeginblock?

    //储存属性
    @objc var placeholderGlobal:String?{      //提示文字
        didSet{
            plaleLabel.text = placeholderGlobal
            plaleLabel.sizeToFit()
        }
    }
    @objc var placeholderColorGlobal:UIColor?{
        didSet{
            plaleLabel.textColor = placeholderColorGlobal
        }
    }
    @objc var isReturnHidden:Bool = false     //是否点击返回失去响应
    @objc var isShowCountLabel:Bool = false { //是否显示计算个数的Label
        didSet{
            countLabel.isHidden = !isShowCountLabel
        }
    }
    @objc var limitWords:UInt = 1000             //限制输入个数   默认为999999，不限制输入
    
    //MARK: - 系统方法
    /// PlaceholerTextView 唯一初始化方法
    convenience init(placeholder:String?,placeholderColor:UIColor?,frame: CGRect) {
        self.init(frame: frame)
        setup(placeholder: placeholder, placeholderColor: placeholderColor)
        placeholderGlobal = placeholder
        placeholderColorGlobal = placeholderColor
    }
    
    private override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    //XIB 调用
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup(placeholder: nil, placeholderColor: nil)
    }

}

extension PlaceholderTextView{
    /// placeholder Label Setup
    private func setup(placeholder:String?,placeholderColor:UIColor?){
        palceholdertextView.delegate = self
        self.addSubview(palceholdertextView)
        palceholdertextView.textColor = UIColor.colorWithHex("#7863A6")
        palceholdertextView.backgroundColor = .clear
        palceholdertextView.snp.makeConstraints { make in
            make.leading.top.trailing.equalToSuperview()
            make.bottom.equalToSuperview().offset(-PTW(40))
        }
        if palceholdertextView.font==nil {
            palceholdertextView.font = UIFont.pingfangFont(ofSize: PTW(15), weight: .medium)
        }
        plaleLabel.textColor = placeholderColor
        plaleLabel.textAlignment = .left
        plaleLabel.font = palceholdertextView.font
        plaleLabel.text = placeholder
        plaleLabel.sizeToFit()
        addSubview(plaleLabel)
        plaleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(PTW(25))
            make.top.equalToSuperview().offset(PTW(20))
            make.trailing.equalToSuperview().offset(-PTW(20))
        }
        
        palceholdertextView.textContainerInset = UIEdgeInsets(top: PTW(20), left: PTW(20), bottom: PTW(20), right: PTW(20))
        countLabel.font = UIFont.pingfangFont(ofSize: PTW(15), weight: .medium)
        countLabel.textColor = UIColor.colorWithHex("#DFDFDF")
        addSubview(countLabel)
        countLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-PTW(20))
            make.bottom.equalToSuperview().offset(-PTW(10))
        }
    }
}

//MARK: - UITextViewDelegate代理方法
extension PlaceholderTextView : UITextViewDelegate{
    
    func textViewDidChange(_ textView: UITextView) {
        checkShowHiddenPlaceholder()
        countLabel.text = "\(textView.text.count)/\(limitWords)"
        countLabel.sizeToFit()
    }
    
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        if self.editBeginBack != nil {
            self.editBeginBack!()
        }
        return true
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text=="\n"&&isReturnHidden==true {
            textView.resignFirstResponder()
        }
        //大于等于限制字数，而且不是删除键的时候不可以输入。
        //考虑复制粘贴和光标不在最后得情况
        if (textView.text.count + range.length + text.count) > limitWords && !(text as NSString).isEqual(to: ""){
            return false
        }
        return true
    }
}

//MARK : - 工具方法
extension PlaceholderTextView {
    ///根据textView是否有内容显示placeholder
    private func checkShowHiddenPlaceholder(){
        if self.palceholdertextView.hasText {
            plaleLabel.text = nil
            countLabel.isHidden = false
        }else{
            plaleLabel.text = placeholderGlobal
            countLabel.isHidden = true
        }
    }
    
}
